package com.hm.cms.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hm.cms.auto.SystemEnvConfigProperties;
import com.hm.cms.auto.su.R.SystemConfig;
import com.hm.cms.def.AdvertSiteEnum;
import com.hm.cms.def.ExtLlinkEnum;
import com.hm.cms.service.AdvertService;
import com.hm.cms.service.ArticleService;
import com.hm.cms.service.ExtLinkService;
import com.hm.cms.service.TermTaxonomyService;
import com.hm.cms.vo.HcmsArticleVo;
import com.hm.cms.vo.HcmsHomePageVo;
import com.hm.cms.vo.HcmsPageAdvertVo;
import com.hm.cms.vo.HcmsPageBaseInfoVo;
import com.hm.cms.vo.HcmsPageExtLinkVo;
import com.hm.cms.vo.HcmsTermTaxonomyVo;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageInfoParam;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.EncryptUtil.Base64;
import com.hm.common.util.FreemakUtil;

/**
 * @author shishun.wang
 * @date 下午5:40:14 2017年10月25日
 * @version 1.0
 * @describe 标签链接
 */
@Controller
@RequestMapping("o_term_taxonomy")
public class TermTaxonomyController {

	@Autowired
	private AdvertService advertService;

	@Autowired
	private ExtLinkService extLinkService;

	@Autowired
	private TermTaxonomyService termTaxonomyService;

	@Autowired
	private ArticleService articleService;

	@Autowired
	private SystemEnvConfigProperties systemEnvConfigProperties;
	
	@RequestMapping(value = "/tags/{termTayCode}", method = RequestMethod.GET)
	public String queryTagsTermTaxonomyArticles(@PathVariable("termTayCode") String termTayCode, Model model) {
		HcmsTermTaxonomyVo termTaxonomyVo = termTaxonomyService.getTermTaxonomysByCode(termTayCode);
		model.addAttribute("termTay", termTaxonomyVo);
		
		this.initBaseInfo(model, termTaxonomyVo.getId());
		this.setHeadPageAdvert(AdvertSiteEnum.HOME_HEAD_SLIDE, model);
		this.setExtLink(model);
		model.addAttribute("moreArticlCatalogUri", "o_term_taxonomy/pagging/show/"+termTaxonomyVo.getId());
		
		HcmsHomePageVo homePageVo = new HcmsHomePageVo();
		{
			homePageVo.setWebPageNavs(termTaxonomyService.loadSysNavs());
		}
		model.addAttribute("homePageInfo", homePageVo);
		
		return "article/article_list";
	}
	
	@RequestMapping(value = "/{termTayId}", method = RequestMethod.GET)
	public String queryTermArticles(@PathVariable("termTayId") String termTayId, Model model) {
		this.initBaseInfo(model, termTayId);
		this.setHeadPageAdvert(AdvertSiteEnum.HOME_HEAD_SLIDE, model);
		this.setExtLink(model);
		
		HcmsTermTaxonomyVo termTaxonomyVo = termTaxonomyService.getTermTaxonomysById(termTayId);
		model.addAttribute("termTay", termTaxonomyVo);
		model.addAttribute("moreArticlCatalogUri", "o_term_taxonomy/pagging/show/"+termTayId);
		
		HcmsHomePageVo homePageVo = new HcmsHomePageVo();
		{
			homePageVo.setWebPageNavs(termTaxonomyService.loadSysNavs());
		}
		model.addAttribute("homePageInfo", homePageVo);
		model.addAttribute("basePath", systemEnvConfigProperties.getStaticHtmlDomainName());
		
		return "article/article_list";
	}
	
	@RequestMapping(value = "/pagging/show/{termTayId}", method = RequestMethod.GET)
	public void paggingTermArticles(@PathVariable("termTayId") String termTayId,
			@RequestParam(name="pageNumber") Integer pageNumber, HttpServletResponse response) {

		try {
			PageInfo<HcmsArticleVo> pageInfo = articleService.queryArticle(new PageInfoParam(pageNumber, SystemConfig.ARTICLE_CATALOG_SHOW_SIZE), termTayId,
					null);
			if(CommonUtil.isEmpty(pageInfo)){
				response.getWriter().write("没有更多内容了....");
				return;
			}

			HcmsPageBaseInfoVo baseInfoVo = new HcmsPageBaseInfoVo();
			baseInfoVo.newestArticle(pageInfo);
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			// 这句话的意思，是告诉servlet用UTF-8转码，而不是用默认的ISO8859
			response.setCharacterEncoding("UTF-8");

			Resource resource = new ClassPathResource("/templates/common");
			File file = resource.getFile();
			
			Map<String, Object> mapper = new HashMap<String, Object>();
			mapper.put("baseInfo", baseInfoVo);
			mapper.put("basePath", systemEnvConfigProperties.getStaticHtmlDomainName());
			
			FreemakUtil.process(file, "article_catalog_view.ftl", mapper, response.getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void initBaseInfo(Model model, String termTayId) {
		HcmsPageBaseInfoVo baseInfoVo = new HcmsPageBaseInfoVo();
		{
			baseInfoVo.setOpenLogin(systemEnvConfigProperties.isOpenLogin());
			baseInfoVo.setSiteFtInfo(new String(Base64.decode(systemEnvConfigProperties.getSiteFooterInfo().toCharArray())));
			baseInfoVo.setSiteShare(new String(Base64.decode(systemEnvConfigProperties.getSiteShare().toCharArray())));
			baseInfoVo.termTaxonomy(termTaxonomyService.loadHcmsTermTaxonomys());

			baseInfoVo.newestArticle(
					articleService.queryArticle(new PageInfoParam(1, SystemConfig.ARTICLE_CATALOG_SHOW_SIZE), termTayId, null));
			baseInfoVo.hotArticle(articleService.queryArticle(new PageInfoParam(1, SystemConfig.TAG_SHOW_SIZE), null, null));
		}

		model.addAttribute("baseInfo", baseInfoVo);
	}

	private void setExtLink(Model model) {
		model.addAttribute("pageExtLlink", new HcmsPageExtLinkVo().friendshipLink(extLinkService.loadExtLinks(ExtLlinkEnum.FRIENDSHIP_LINK))
				.onlineToolLink(extLinkService.loadExtLinks(ExtLlinkEnum.ONLINE_TOOL_LINK)));
	}

	private void setHeadPageAdvert(AdvertSiteEnum advertSite, Model model) {
		model.addAttribute("pageAdvert", new HcmsPageAdvertVo().headSlides(advertService.loadPageAdvert(advertSite)));
	}
}
